package com.lc.pianostudent.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lc.pianostudent.R;
import com.lc.pianostudent.bean.HomeNewsBean;
import com.lc.pianostudent.bean.ShopBean;
import com.lc.pianostudent.conn.Conn;
import com.squareup.picasso.Picasso;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/25.
 */

public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder>{


    private Context context;

    private List<ShopBean> list = new ArrayList<>();

    public ShopAdapter(Context context, List<ShopBean> list) {
        this.context = context;
        this.list = list;
    }

    public interface OnItemClickListener {

        void onItemClick(View view, int position);

    }

    private ShopAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(ShopAdapter.OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shop, parent, false);

        ScaleScreenHelperFactory.getInstance().loadViewGroup((ViewGroup) view);

        ShopAdapter.ViewHolder viewHolder = new ShopAdapter.ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.money.setText(list.get(position).getPrice()+"琴币");

        Log.e("====show",list.get(position).getPicshow());
//        Glide.with(context).load(list.get(position).getPicshow()).into(holder.img);
//        Picasso.with(context).load(list.get(position).getPicshow()).into(holder.img);
        GlideLoader.getInstance().get(list.get(position).getPicshow(),holder.img,R.mipmap.goods_bg);

        if(list.get(position).isFlag()){
            holder.goods_bg.setBackgroundResource(R.drawable.layer_pink_solid_gray_top_right_bottom);
        }else {
            holder.goods_bg.setBackgroundResource(R.drawable.layer_qian_pink_solid_gray_top_right_bottom);
        }
        setUpItemEvent(holder,position);

    }

    //为RecyclerView添加点击事件
    protected void setUpItemEvent(final ShopAdapter.ViewHolder holder, final  int position) {

        if (onItemClickListener != null) {

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //  int LayoutPosition = holder.getLayoutPosition();

                    onItemClickListener.onItemClick(holder.itemView, position);



                }

            });

        }

    }
    @Override
    public int getItemCount() {
        return list.size();
    }


    //初始化组件
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView money;
        public ImageView img;
        public RelativeLayout goods_bg;

        public ViewHolder(View itemView) {
            super(itemView);

            money = (TextView) itemView.findViewById(R.id.shop_money);
            img = itemView.findViewById(R.id.shop_img);
            goods_bg = itemView.findViewById(R.id.goods_bg);

        }
    }
}
